package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TechCapacityRuleServiceImpl implements TechCapacityRuleService {

    @Autowired
    private TechCapacityRuleDao techCapacityRuleDao;

    @Autowired
    private BaseCapacityRuleDao baseCapacityRuleDao;

    @Autowired
    private PcbaLineRelatedDeviceDao pcbaLineRelatedDeviceResourceDao;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private LineTechBigTypeDao lineTechBigTypeDao;

    @Autowired
    private LineTechSmallTypeDao lineTechSmallTypeDao;

    @Autowired
    private LineBaseBigTypeDao lineBaseBigTypeDao;

    @Autowired
    private LineBaseSmallTypeDao lineBaseSmallTypeDao;

    @Autowired
    private PcbaDeviceParamTypeDao pcbaDeviceParamTypeDao;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "工艺能力规则";

    @Override
    public List<TechCapacityRule> getAllTechCapacityRule(String searchValue, Integer lineTechBigTypeId, Integer lineTechSmallTypeId) {
        List<TechCapacityRule> list = techCapacityRuleDao.getAllTechCapacityRule(searchValue, lineTechBigTypeId, lineTechSmallTypeId);
        for (TechCapacityRule techCapacityRule : list) {
            LineTechBigType lineTechBigTypeById = lineTechBigTypeDao.getLineTechBigTypeById(techCapacityRule.getLineTechBigTypeId());
            techCapacityRule.setLineTechBigType(lineTechBigTypeById);

            LineTechSmallType lineTechSmallTypeById = lineTechSmallTypeDao.getLineTechSmallTypeById(techCapacityRule.getLineTechSmallTypeId());
            techCapacityRule.setLineTechSmallType(lineTechSmallTypeById);

            if (techCapacityRule.getBaseCapacityParamKey() != null && !"".equals(techCapacityRule.getBaseCapacityParamKey())) {
                if(techCapacityRule.getTechCapacityRuleType() == 4 || techCapacityRule.getTechCapacityRuleType() == 5 || techCapacityRule.getTechCapacityRuleType() == 6 || techCapacityRule.getTechCapacityRuleType() == 7){
                    // 查询设备基线能力值
                    String BaseCapacityParamKeys = techCapacityRule.getBaseCapacityParamKey();
                    String[] split = BaseCapacityParamKeys.split(",");
                    List<PcbaDeviceParam> lis = new ArrayList<>();
                    for (String s : split) {
                        PcbaDeviceParam pcbaDeviceParam = pcbaDeviceParamDao.getPcbaDeviceParamByKey(s);
                        PcbaDeviceParamType pcbaDeviceParamType = pcbaDeviceParamTypeDao.getPcbaDeviceParamTypeById(pcbaDeviceParam.getPcbaDeviceParamTypeId());
                        pcbaDeviceParamType.setPcbaDeviceType(pcbaDeviceTypeDao.getById(pcbaDeviceParamType.getPcbaDeviceTypeId()));
                        pcbaDeviceParam.setPcbaDeviceParamType(pcbaDeviceParamType);
                        lis.add(pcbaDeviceParam);
                    }
                    techCapacityRule.setPcbaDeviceParamList(lis);
                }else{
                    BaseCapacityRule baseCapacityRuleByKey = baseCapacityRuleDao.getBaseCapacityRuleByKey(techCapacityRule.getBaseCapacityParamKey());
                    if(baseCapacityRuleByKey != null){
                        baseCapacityRuleByKey.setLineBaseBigType(lineBaseBigTypeDao.getLineBaseBigTypeById(baseCapacityRuleByKey.getLineBaseBigTypeId()));
                        baseCapacityRuleByKey.setLineBaseSmallType(lineBaseSmallTypeDao.getLineBaseSmallTypeById(baseCapacityRuleByKey.getLineBaseSmallTypeId()));
                        techCapacityRule.setBaseCapacityRule(baseCapacityRuleByKey);
                    }
                }
            }
        }
        return list;
    }

    @Override
    public Result updateTechCapacityRule(TechCapacityRule techCapacityRule) {
        try {

            String userId = UserHeaderHolder.getUserId();
            TechCapacityRule oldTechCapacityRule = techCapacityRuleDao.getTechCapacityRuleById(techCapacityRule.getTechCapacityRuleId());

            techCapacityRule.setUpdateTime(new Date());
            Integer integer = techCapacityRuleDao.updateTechCapacityRule(techCapacityRule);
            if (integer > 0) {

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldTechCapacityRule), JSONObject.toJSONString(techCapacityRule)));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdateTechCapacityRule(String mode, TechCapacityRule techCapacityRule) {
        try {
            if (techCapacityRule == null) {
                return ResultUtil.error(500, "缺少参数techCapacityRule", "Miss Param techCapacityRule", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }
            if ("edit".equals(mode)) {
                if (techCapacityRule.getTechCapacityRuleId() == null || "".equals(techCapacityRule.getTechCapacityRuleId().toString())) {
                    return ResultUtil.error(500, "缺少参数techCapacityRuleId", "Miss Param techCapacityRuleId", null);
                }

                List<TechCapacityRule> techCapacityRuleList = techCapacityRuleDao.judgeUniqueTechCapacityRule(techCapacityRule.getLineTechBigTypeId(), techCapacityRule.getLineTechSmallTypeId(), techCapacityRule.getTechCapacityRuleId(), techCapacityRule.getTechCapacityRuleName());
                if (techCapacityRuleList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                techCapacityRule.setUpdateTime(new Date());
                Integer integer = techCapacityRuleDao.updateTechCapacityRule(techCapacityRule);
                if (integer > 0) {
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                List<TechCapacityRule> techCapacityRuleByNameAndType = techCapacityRuleDao.getTechCapacityRuleByNameAndType(techCapacityRule.getTechCapacityRuleName(), techCapacityRule.getLineTechBigTypeId(), techCapacityRule.getLineTechSmallTypeId());
                if (techCapacityRuleByNameAndType.size() > 0) {
                    return ResultUtil.error(500, "该项已存在", "Already Exist", null);
                }

                techCapacityRule.setCreateTime(new Date());
                techCapacityRule.setActualEqualDeclare(2);
                Integer integer = techCapacityRuleDao.addTechCapacityRule(techCapacityRule);
                if (integer > 0) {
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result deleteTechCapacityRule(Integer techCapacityRuleId) {
        try {
            if (techCapacityRuleId == null || "".equals(techCapacityRuleId.toString())) {
                return ResultUtil.error(500, "缺少参数techCapacityRuleId", "Miss Param techCapacityRuleId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            TechCapacityRule oldTechCapacityRule = techCapacityRuleDao.getTechCapacityRuleById(techCapacityRuleId);

            if (oldTechCapacityRule.getIsRelatedPcba() != null && oldTechCapacityRule.getIsRelatedPcba() == 1) {
                return ResultUtil.error(500, "参与pcba能力匹配，禁止删除", "Forbidden Delete", null);
            }

            Integer integer = techCapacityRuleDao.deleteTechCapacityRuleById(techCapacityRuleId);
            if (integer > 0) {

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldTechCapacityRule), null));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result changeActualEqualDeclare(Integer techCapacityRuleId) {
        try {
            if (techCapacityRuleId == null || "".equals(techCapacityRuleId.toString())) {
                return ResultUtil.error(500, "缺少参数techCapacityRuleId", "Miss Param techCapacityRuleId", null);
            }

            String userId = UserHeaderHolder.getUserId();

            TechCapacityRule techCapacityRule = techCapacityRuleDao.getTechCapacityRuleById(techCapacityRuleId);
            if (techCapacityRule != null) {
                Integer actualEqualDeclare1 = techCapacityRule.getActualEqualDeclare();
                Integer newActualEqualDeclare = null;
                if (actualEqualDeclare1 == null || !"1".equals(actualEqualDeclare1.toString())) {
                    newActualEqualDeclare = 1;
                }
                Integer count = techCapacityRuleDao.changeActualEqualDeclare(techCapacityRuleId, newActualEqualDeclare);
                if (count > 0) {

                    TechCapacityRule newTechCapacityRule = techCapacityRule;
                    newTechCapacityRule.setActualEqualDeclare(newActualEqualDeclare);
                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(techCapacityRule), JSONObject.toJSONString(newTechCapacityRule)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
            return ResultUtil.error(500, "未找到TechCapacityRule", "Not Found TechCapacityRule", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
